package com.ctg.itrdc.k8sconsole.service.impl;

import java.net.URL;
import java.text.MessageFormat;

import javax.annotation.Resource;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.common.util.HttpRequestUtil;
import com.ctg.itrdc.imageconsole.data.dao.IClusterConfigDao;
import com.ctg.itrdc.imageconsole.data.entity.ClusterConfig;
import com.ctg.itrdc.k8sconsole.service.ILogService;
import com.ctg.itrdc.paspsdk.common.utils.file.PropertiesUtil;

/**
 * 
 * @description: 
 * @version: v1.0.0 
 * @author: Piao.Wu
 * 
 * Modification History:
 * Date               Author      Version     Description
 * -----------------------------------------------------------------
 * 2016年8月23日 下午7:41:31     Piao.Wu       v1.0.0        create
 *
 */
@Component("logService")
public class LogServiceImpl implements ILogService{

	@Resource
    private IClusterConfigDao clusterConfigDao;
	
	@Override
	public String getPodLog(String clusterCode,String nameSpace, String podName,String perpage,String page) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);
		
		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties","getPodLog.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode,nameSpace,podName);
		// 创建URL连接
        URL url = new URL(urlString);
        HttpRequestUtil requestUtil = new HttpRequestUtil(url);
        WebTarget target = requestUtil.target;
        Response response = target.queryParam("perpage", perpage).queryParam("page", page).request().get();
		
        return requestUtil.getResponseJsonData(response);
	}

	
	@Override
	public String getContainerLog(String clusterCode,String nameSpace, String podName,
			String containerName,String perpage,String page) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);
		
		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties","getContainerLog.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode,nameSpace,podName,containerName);
		// 创建URL连接
        URL url = new URL(urlString);
        HttpRequestUtil requestUtil = new HttpRequestUtil(url);
        WebTarget target = requestUtil.target;
        Response response = target.queryParam("perpage", perpage).queryParam("page", page).request().get();
		
        return requestUtil.getResponseJsonData(response);
	}
	
}
